package com.followme.module.common;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: DataBase util
 * Date: 05.12.13
 * Time: 20:18
 */
public class DbSqlLiteUtil {

    private void CopyDB(InputStream inputStream, OutputStream outputStream)
            throws IOException {

        //---copy 1K bytes at a time---
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }

        inputStream.close();
        outputStream.close();
    }

    public void copyDbFromAssets(String packagePath, String nameDb,
                                 InputStream assetsPathDb){

        String destDir = "/data/data/" + packagePath +"/databases/";
        String destPath = destDir + nameDb;
        File f = new File(destPath);
        if (!f.exists()) {
            File directory = new File(destDir);
            directory.mkdirs();

            try {
                CopyDB(assetsPathDb, new FileOutputStream(destPath));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
